# coding=utf-8
'''
Created on 2019年1月16日

@author: zy
'''
from uiautomator import Device as d
import tempfile
from retrying import retry
from traverse.common.log import Logger

# 收集异常信息
def collectException(func):
    def wrap(*args, **argkw):
        try:
            return func(*args, **argkw)
        except:
            pass
    return wrap

class Driver(object):
    
    def __init__(self, serial=None):
        self.serial = serial
        self.logger = Logger.getLogger()
        self.device = d(serial)
        self.width = None
        self.height = None
        self.__info_init()
    
    def __info_init(self):
        info = self.device.info
        self.width = info['displayWidth']
        self.height = info['displayHeight']
        
    def refresh_page(self):
        if len(self.device._get_current_page()) > 100:
            return True
        return False
    
    @collectException
    def get_current_page(self):
        return self._get_current_page()
        
    @retry(stop_max_attempt_number=3)
    def _get_current_page(self):
        tmp = tempfile.mktemp('crawler')
        source = self.device.dump(tmp)
#         print "page: " + source
        if len(source) < 100:
            raise RuntimeError('get page_source failure')
        return tmp
    
    @collectException
    def start_app(self, package_name):
        self.logger.info("start app: "+ package_name)
        self.device.adb.cmd("shell", "am", "start", package_name).wait()
    
    def force_stop(self,package_name):
        self.logger.info("force-stop: "+ package_name)
        self.device.adb.shell('am','force-stop',package_name)
    
    @collectException
    def screenShot(self, file_name):
        self.logger.info("screenshot to %s"%file_name) 
        self.device.screenshot(file_name)
    
    @collectException
    def get_current_activity(self):
        activity = self.device.adb.current_app()[1]
        self.logger.info("get current activity:" + activity) 
        return activity
    
    @collectException
    def get_current_app(self):
        app = self.device.adb.current_app()[0]
        self.logger.info("current app:" + app) 
        return app
    
    @collectException
    def clear_forder(self, app_path):
        self.logger.info("clear app_path:" + app_path) 
        self.device.adb.shell('rm','-rf',app_path)
    
#     @collectException
    def click(self, x, y):
        self.logger.info("action click %s %s"%(x,y)) 
        self.device.click(x, y)
    
    @collectException
    def long_click(self, x, y):
        self.logger.info("action long click %s %s"%(x,y)) 
        self.device.long_click(x, y)
    
    @collectException   
    def back(self):
        self.logger.info("action back") 
        self.device.press.back()
    
    @collectException   
    def page_up(self):
        self.logger.info("action page_up")  
        self.device().swipe.up()
    
    @collectException  
    def page_left(self):
        self.logger.info("action page_left")  
        self.device().swipe.left()
    
    @collectException     
    def page_down(self):
        self.logger.info("action page_down")       
        self.device().swipe.down()
    
    @collectException     
    def page_right(self):
        self.logger.info("action page_right")  
        self.device().swipe.right()

if __name__ == '__main__':
    dd = Driver()
    print dd.get_current_activity()